/**
 * 医疗服务质询初始化模块，提供医疗服务编辑页面初始化组件功能
 * 
 * @Date: 2016-1-25
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */

define(["jquery","sysmanMain","valueUtil","uploadfiles","deptUserSelect","formServ"],
		function($,sysmanMain,valueUtil,uploadfiles,deptUserSelect,formServ) {
			
		'use strict';
		
		var edit_window_id='ethic-gift-win'		
		
		var rootid = 'panel-ethic-gift'
		var public_method ={
			
			beforeSubmit:function(){
				
				var _form = $('#'+edit_window_id +' .main-form')
				
				var _integritydate = $('#'+edit_window_id +' #integritydate')
				if(_integritydate.length ==1){
					var _tmpValue = $(_integritydate).datebox('getValue')
					if(_tmpValue){
						$(_integritydate).val(_tmpValue)
					}
				}
				var valid =$(_form).form('enableValidation').form('validate');
				if(!valid){
					
					 return valid
				}
				var _moduleConfig = sysmanMain.getCurrentModuConfig();
				var _id = $('#'+edit_window_id + ' #id')
				if(_id.length==1){
					var idVal = $(_id).val()
					if(idVal){
						if(idVal.indexOf('TMP')>=0){
							$(_id).val('')
						}
					}
					
				}
				return {bizcode:_moduleConfig.bizcode}
			},
			
			rowClick:function(index,data){
				
				if(window.console)
					console.info('inpatient rowClick event fired ...')
				var _config = sysmanMain.getModuConf('inpatientItemFee');
				if(_config ==null){
					_config = {}					
				}	
				
				_config.initQueryParams = {field:'patient_id',fieldVal:data.patient_id}
				sysmanMain.setModuConf('inpatientItemFee',_config)
				sysmanMain.medicalInpatientItemFeeSelect()
			},
			/**
			 * 初始化新增页面
			 * @param _options.record 列表选中行
			 * @param _options.form 页面form对象
			 * @param _options.rootId 页面根节点
			 */
			initElement:function(_options){
				var _rootpath = '/'+sysmanMain.getRootPath()
				
				var deptQueryUrl='/department/query/getTreeList.action'
				//判断form对象是否存在
				if(_options && _options.rootId){
					var _table = $('#' + _options.rootId + ' table.dg-list')
					if(_table.length==1){
						$(_table).datagrid({
							onClickCell:private_method.clickCell
						})
					}
				}
			}
			,
			initEdit:function(_options){
				private_method.initEdit(_options)
			}
			
		}
		
		var private_method ={
			/**
			 * 
			 */
			initEdit:function(_options){
				
				var _rootpath = '/'+sysmanMain.getRootPath()
				if(window.console){
					console.log(' initEdit loaded ...')
					
				}
				
				//人员选择初始化
				var _el = $('#'+edit_window_id+' #person_name')
				if(_el &&_el.length>0){
					
					$(_el).textbox({
						onClickButton:function(data){
							
							var _userParam = {
								userids:edit_window_id +' #person_code',
								usernames:edit_window_id+' #person_name',
								deptid:edit_window_id +' #org_id',
								deptname:edit_window_id+' #org_name',
								singleSelect:true,
								selectType:'job',
								subsystem:'ethic'
							}
							deptUserSelect.openPage(_userParam)							
						}
					})
				}
				
				//退还方式初始化
				var _mainSelect = $('#'+edit_window_id +' #repayment_id')
				$(_mainSelect).combobox({					
					textField:'name',
					valueField:'id',								
					url:_rootpath + '/dictionary/getTree.action?id=' +130,
					onSelect:function(record){
						var _item = $('#'+edit_window_id +' #repayment')
						$(_item).val(record.name)
						$(_mainSelect).val(record.id)
					}					
				})
				
				var _id = $('#'+edit_window_id +' #id')
				if(_id.length ==1){
					
					
					var _tmpValue = $(_id).val()
					//id为空
					if(!_tmpValue || _tmpValue==''){
						var _date = new Date();
						_date = _date.getFullYear()+''+(_date.getMonth()+1)+''+_date.getDate()+'' +_date.getMilliseconds();
						var _tmpid = $('#'+edit_window_id +' #tmpid')
						$(_tmpid).val('TMP'+_date)
						$(_id).val('TMP'+_date)
					}
				}
				
			},
			clickCell:function(index,field,value){
				if(window.console)
					console.info(' clickCell fired ...')
				
				if(field =='arc_num'){
					var $bizgrid= $('#'+rootid + ' table.dg-list')
					var record = $($bizgrid).datagrid('getRows')[index];				
					var _moduleConfig = sysmanMain.getCurrentModuConfig();
					var rootPath = '/' +sysmanMain.getRootPath()
					var uploadParam = {
								data:{
									bizno:record.id,
									bizcode:_moduleConfig.moduleCode
								},
								rootPath:rootPath							
							}
					uploadfiles.imgShow(uploadParam)
				}
			}
		}
			
		
		return public_method;
			
})